<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;
use Auth;

class ServerRadioComposer
{
    protected $channelService;

    public function __construct()
    {

    }

    public function compose(View $view)
    {
        $reqChannelId = ($view->reqChannelId == null ? old('channel_id') : $view->reqChannelId);
        $reqServerId = ($view->reqServerId == null ? old('server_id') : $view->reqServerId);
        $servers = Auth::guard('admin')->user()->getServers(['game_id' => session('game_id')], 'channel_id');

        //拼出 渠道table
        $serverHtml = '';
        foreach ($servers as $server) {
            $serverId = $server['server_id'];
            $channel_name = $server['channel_name'];
            $channel_id = $server['channel_id'];
            $server_name = $server['server_name'];
            $checked = '';
            $show = 'display:inline-block;';

            if (empty($reqChannelId) || $server['channel_id'] != $reqChannelId) {
                $show = 'display:none;';
            }

            if (isset($reqServerId) && $channel_id == $reqChannelId && $serverId == $reqServerId) {
                $checked = 'checked="checked"';
            }

            $serverHtml .= <<<EOF
 <div class="col-sm-2" style="min-width: 20rem;$show" data-channel-id="$channel_id">
     <label>&emsp;$server_name
            <input class="pull-left" type="radio" name="server_id" $checked value="$serverId">
    </label>
</div>
EOF;

        }

        $view->with(compact('serverHtml'));
    }
}